package com.tappytaps.android.hc2010;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.util.Log;
import android.widget.ArrayAdapter;


public abstract class VoiceRecognitionActivity extends Activity {
	
	public boolean ttsSupported; 
	private static final int VOICE_RECOGNITION_REQUEST_CODE = 4321;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		 PackageManager pm = getPackageManager();
	        List<ResolveInfo> activities = pm.queryIntentActivities(
	                new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
	        Log.i("XX",activities.toString());
	}
	
	protected void startVoiceRecognitionActivity(String message, String language) {	
		 Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
	     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
	             RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
	     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
	     intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
	     startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
	}
	
	abstract void voiceRecognized(String s);
	
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
            // Fill the list view with the strings the recognizer thought it could have heard
            ArrayList<String> matches = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);
            voiceRecognized(matches.get(0));
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
